//
//  JSLWarnRepmentCell.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/5.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLWarnRepmentCell.h"


@interface JSLWarnRepmentCell ()
@property (nonatomic , weak) UIView *colorView;
@property (nonatomic , weak) UILabel *nameLab;
@property (nonatomic , strong) NSArray *textArray;
@property (nonatomic , weak) UILabel *stateLab;
@end
@implementation JSLWarnRepmentCell

#pragma mark - init
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        [self setUpAllChildView];
        
    }
    return self;
}
- (void)setInfo:(NSDictionary *)info {
    _info = info;
    //    @"name":@"百度有钱花",
    //    @"money":@"3000",
    //    @"moneylixi":@"0.02",
    //    @"loanTime":@"2018-05-22",
    //    @"repaymentTime":@"2018-06-22",
    NSInteger timestamp_now = [self getNowTimestamp];
    
    NSInteger timestamp_some = [self timeSwitchTimestamp:info[@"repaymentTime"] andFormatter:@"YYYY-MM-dd"];
    
    NSString *days = @"";
    NSString *state = @"";
    if (timestamp_now>timestamp_some) {
        days=@"0天";
        state = @"已还款";
    }else if (timestamp_now == timestamp_some){
        days=@"0天";
        state = @"今天还款";
    }else {
        days = [NSString stringWithFormat:@"%ld天",(timestamp_some-timestamp_now)/(24 *3600)];
        state = @"未还款";
    }
    
    
    CGFloat lixiValue = [info[@"moneylixi"] floatValue]*0.01 *[info[@"loanTime"] integerValue]*[info[@"money"] floatValue];
    
    CGFloat totleValue = lixiValue +[info[@"money"] floatValue];
    
    self.nameLab.text = info[@"name"];
    self.stateLab.text =state;
    self.textArray = @[[NSString stringWithFormat:@"%.2f",totleValue],info[@"repaymentTime"],days];
}


-(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    return timeSp;
    
}
#pragma mark - 获取当前时间的 时间戳

-(NSInteger)getNowTimestamp{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间
    
    NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}

#pragma mark - 配置视图
- (void)layoutSubviews{
    [super layoutSubviews];
    [self configFrame];
}

- (void)configFrame{
    self.colorView.frame = CGRectMake(10 *AutoSizeScaleXX, 15*AutoSizeScaleYY, 4*AutoSizeScaleXX, 13*AutoSizeScaleYY);
    
    
    CGSize nameSize = [self.nameLab.text sizeWithAttributes:@{NSFontAttributeName : self.nameLab.font}];
    self.nameLab.frame = CGRectMake(self.colorView.right +10*AutoSizeScaleXX, 14.5*AutoSizeScaleYY, nameSize.width, nameSize.height);
    
    CGSize stateSize = [self.stateLab.text sizeWithAttributes:@{NSFontAttributeName : self.stateLab.font}];
    self.stateLab.frame = CGRectMake(SCREEN_WIDTH - 10*AutoSizeScaleXX-stateSize.width, self.nameLab.top, stateSize.width, stateSize.height);
    self.stateLab.centerY = self.nameLab.centerY;
    
    self.nameLab.centerY = self.colorView.centerY;
    
    for (NSInteger i = 0; i < self.textArray.count; i ++) {
        UILabel *textLab = [self.contentView viewWithTag:100 +i];
        textLab.frame = CGRectMake(0 +(self.contentView.width/3)*i, self.nameLab.bottom+10*AutoSizeScaleYY, self.contentView.width/3, 20);
        
        textLab.text = self.textArray[i];
        
        UILabel *contenLab = [self.contentView viewWithTag:1000 +i];
        contenLab.frame = CGRectMake(0 +(self.contentView.width/3)*i, textLab.bottom+2*AutoSizeScaleXX, self.contentView.width/3, 20);
        
        
        
    }
    
}
- (void)setUpAllChildView{
    self.colorView = [JSLKitFactory jsl_viewWithFrame:CGRectZero backgroundColor:[@"#338cff" hexStringToColor] superView:self.contentView];
    
    self.nameLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"" font:MEDIUM_FONT(14) textColor:[@"#333333" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.contentView];
    
    self.stateLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"" font:LIGHT_FONT(12) textColor:[@"#FC5B35" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.contentView];
    
    NSArray *textArray = @[@"应还金额(元)",@"应还日期",@"距离应还天数"];
    NSArray *contentArray = @[@"--",@"--",@"--"];
    for (NSInteger i = 0; i <3 ; i ++) {
        UILabel *textlab =  [JSLKitFactory jsl_labelWithFrame:CGRectZero text:contentArray[i] font:MEDIUM_FONT(16) textColor:[@"#333333" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.contentView];
        textlab.tag = i +100;
        
        UILabel *contenLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:textArray[i] font:LIGHT_FONT(10) textColor:[@"#999999" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.contentView];
        contenLab.tag = i +1000;
    }
}

#pragma mark - 创建cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * ID = @"JSLWarnRepmentCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
